package lacus.wd_cinema.controller.views;

import jakarta.servlet.http.HttpServletRequest;
import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.service.AuthService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class AuthViewController {

    private final AuthService authService;

    public AuthViewController(AuthService authService) {
        this.authService = authService;
    }

    /** 用户登录页面 */
    @GetMapping("/login")
    public String getLoginPage() {
        return "auth/LoginPage";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest servletRequest) {
        authService.logout(servletRequest);
        return "redirect:/login";
    }

    @GetMapping("/register")
    public String getRegisterPage() {
        return "auth/RegisterPage";
    }

    @GetMapping("/home")
    public String loginSuccessfully(HttpServletRequest servletRequest) {
        User user = authService.getCurrentUser(servletRequest);
        if (user.isAdmin()) {
            return "redirect:/admin";
        } else if (user.isManager()) {
            return "redirect:/manager";
        } else {
            return "redirect:/user";
        }
    }
}
